package com.zhanbo.zhanbowymusic.main.featured;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import com.bigkoo.convenientbanner.listener.CBPageChangeListener;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.mwx.baselib.base.BaseFragment;
import com.mwx.baselib.bean.SingerListBean;
import com.mwx.baselib.utils.GlideUtils;
import com.mwx.baselib.utils.MLog;
import com.zhanbo.zhanbowymusic.R;
import com.zhanbo.zhanbowymusic.databinding.FragmentSingerListDetailsBinding;
import com.zhanbo.zhanbowymusic.main.mine.recently.SongListRecentlyFragment;

import java.util.ArrayList;

public class SingerListDetailsFragment extends BaseFragment<FragmentSingerListDetailsBinding> {
    private String[] mTitlesArrays = {"歌曲", "专辑", "MV"};
    private int singerId;
    private SingerSongFragment singerSongFragment;
    private SingerAlbumFragment singerAlbumFragment;
    private SingerMVFragment singerMVFragment;

    public static SingerListDetailsFragment newInstance(SingerListBean.ItemsDTO itemsDTO) {
        Bundle args = new Bundle();
        args.putSerializable("data",itemsDTO);
        SingerListDetailsFragment fragment = new SingerListDetailsFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected Object getLayout() {

        return R.layout.fragment_singer_list_details;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        initData();
        initView();
        initListener();

    }


    @Override
    protected void initData() {
        if (getArguments()!=null){
            SingerListBean.ItemsDTO data= (SingerListBean.ItemsDTO) getArguments().getSerializable("data");
            singerId = data.getId();
            binding.tvName.setText(data.getName()+"");
            GlideUtils.loadImage(_mActivity, data.getCoverImg(), binding.ivHead);
        }

    }

    @Override
    protected void initView() {
        singerSongFragment = SingerSongFragment.newInstance(singerId);
        singerAlbumFragment = SingerAlbumFragment.newInstance(singerId);
        singerMVFragment = SingerMVFragment.newInstance(singerId);
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(singerSongFragment);
        fragments.add(singerAlbumFragment);
        fragments.add(singerMVFragment);
        binding.vpSingerDetails.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }
        });
        binding.tabSingerDetails.setViewPager(binding.vpSingerDetails, mTitlesArrays);
        binding.vpSingerDetails.setOffscreenPageLimit(3);
    }

    @Override
    protected void initListener() {
        binding.btnSingerPlayAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //全部播放
                singerSongFragment.playAll();
            }
        });
        binding.tabSingerDetails.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                binding.btnSingerPlayAll.setVisibility(View.GONE);
                if (position == 0){
                    binding.btnSingerPlayAll.setVisibility(View.VISIBLE);
                }
            }
            @Override
            public void onTabReselect(int position) {

            }
        });
        binding.iconBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pop();
            }
        });
        binding.vpSingerDetails.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position==0){
                    binding.btnSingerPlayAll.setVisibility(View.VISIBLE);
                }else {
                    binding.btnSingerPlayAll.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    @Override
    public void onDestroy() {
        MLog.e("SingerListDetailsFragment----onDestroy");
        super.onDestroy();
    }
}
